package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.AddressBookDto;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;
    //请求网址: http://localhost:8080/addressBook/list
    //请求方法: GET

    /**
     * 根据登录的用户id，查询其地址信息
     * 界面展示
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R<List> list(HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }
    //请求网址: http://localhost:8080/addressBook
    //请求方法: POST

    /**
     * 添加地址信息
     * @param request
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody AddressBook addressBook){
        Long userId = (Long) request.getSession().getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加成功！");
    }
    //请求网址: http://localhost:8080/addressBook/1631894439565799426
    //请求方法: GET

    /**
     * 点击修改，按照id展示地址信息
     * @param addressId
     * @return
     */
    @GetMapping("/{addressId}")
    public R<AddressBookDto> getAddrss(@PathVariable Long addressId){
        AddressBook addressBook = addressBookService.getById(addressId);
        AddressBookDto  addressBookDto = new AddressBookDto();
        BeanUtils.copyProperties(addressBook,addressBookDto);
        int activeIndex = 0;
        String lable = addressBook.getLabel();
        if("公司".equals(lable)){
            activeIndex = 1;
        }else if("家".equals(lable)){
            activeIndex = 2;
        }else if("学校".equals(lable)){
            activeIndex = 3;
        }
        addressBookDto.setActiveIndex(activeIndex);
        return R.success(addressBookDto);
    }

    //请求网址: http://localhost:8080/addressBook
    //请求方法: PUT

    /**
     * 修改地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功！");
    }
    //请求网址: http://localhost:8080/addressBook/default
    //请求方法: PUT

    /**
     * 根据id设置默认地址
     * @param map
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefaultAddress(@RequestBody Map<String,Long> map){
        //只能选择一个设置
        Long id = map.get("id");
        AddressBook addressBook = addressBookService.getById(id);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        //其他的设置为0
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(AddressBook::getId,id);
        List<AddressBook> neList = addressBookService.list(queryWrapper);
        neList = neList.stream().map(item -> {
            item.setIsDefault(0);
            return item;
        }).collect(Collectors.toList());
        addressBookService.updateBatchById(neList);
        return R.success("设置成功！");
    }
    //请求网址: http://localhost:8080/addressBook?ids=1417414526093082626
    //请求方法: DELETE

    /**
     * 按照id删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long []ids){
        for (Long id : ids) {
            addressBookService.removeById(id);
        }
        return R.success("删除成功！");
    }

    //请求网址: http://localhost:8080/addressBook/default
    //请求方法: GET
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return R.success(addressBook);
    }
}
